namespace Tycho.Parser.Peg {
    public class AssociationSwapper {
        private readonly string First;
        private readonly string Last;

        public AssociationSwapper (string first, string last) {
            First = first;
            Last = last;
        }

        public CompositeTerm Swap (CompositeTerm outer) {
            Term inner = outer.SubTerms[Last];
            if (inner is CompositeTerm) {
                var compositeInner = inner as CompositeTerm;

                if (compositeInner.Name == outer.Name) {
                    Term toSwap = compositeInner.SubTerms[First];
                    outer.SubTerms[Last] = toSwap;
                    compositeInner.SubTerms[First] = outer;

                    return compositeInner;
                }
            }

            return outer;
        }
    }
}